<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/11/7
 * Time: 9:52 AM
 */
namespace app\api\job;
use app\api\controller\ApiPrint;
use app\common\model\Agent;
use app\common\model\Merchant;
use think\queue\Job;

class Start
{
    public function fire(Job $job, $data)
    {

        $isJobDone = $this->task_send_print($data);
        $job->delete();

        if ($isJobDone) {
            //如果任务执行成功， 记得删除任务
            $job->delete();
            return '完成';
        } else {
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                print("<warn>任务已完成3次!" . "</warn>\n");
                $job->delete();
                // 也可以重新发布这个任务
                //print("<info>Hello Jobmach will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    }

    public  function task_send_print($param)
    {

        $client_id = $param['client_id'];
        $device_id = $param['device_id'];
        $wsy_printset = $this->getprintid($param['mid']);
        $appid = $wsy_printset['appid'];
        $key = $wsy_printset['key'];
        $ApiPrint = new ApiPrint();

        $keys = $ApiPrint->getparam($client_id, $device_id, $appid, $key);
        $info = $ApiPrint->wsyprint($param, $keys);

        return $info;

    }
    public function getprintid($mid)
    {

        $map['id'] = $mid;

        $name = Merchant::where($map)->value('agent');
        $agentinfo = Agent::where('id', $name)->find();
        $param['appid'] = $agentinfo['wsy_print_appid'];
        $param['key'] = $agentinfo['wsy_print_key'];
        return $param;

    }
}